/**
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

bool HasMethodIdOperandDynamic(AbckitIsaApiDynamicOpcode opcode)
{
    switch(opcode) {
% Panda::instructions.uniq{|i| i.mnemonic}.each do |inst|
%   res = Opcodes.find{|opc| opc.bc_opcode == inst.mnemonic}
%   method_id_operand = inst.operands.find {|op| op.method_id?}
%   if not res.nil? and not method_id_operand.nil?
        case <%= res.abckit_opcode %>:
%   end
% end
            return true;
        default:
            return false;
    }
}

int GetMethodIdOperandIndexDynamic(AbckitIsaApiDynamicOpcode opcode)
{
    switch(opcode) {
% Panda::instructions.uniq{|i| i.mnemonic}.each do |inst|
%   res = Opcodes.find{|opc| opc.bc_opcode == inst.mnemonic}
%   method_id_operand = inst.operands.find {|op| op.method_id?}
%   if not res.nil? and not method_id_operand.nil?
        case <%= res.abckit_opcode %>:
            return <%= inst.operands.find_index(method_id_operand)%>;
%   end
% end
        default:
            return -1;
    }
}

% additional_deprecated_opcodes = ["JNSTRICTEQUNDEFINED",
%                                 "JSTRICTEQUNDEFINED",
%                                 "JNEUNDEFINED",
%                                 "JEQUNDEFINED",
%                                 "JNSTRICTEQNULL",
%                                 "JSTRICTEQNULL",
%                                 "JNENULL",
%                                 "JEQNULL",
%                                 "JNSTRICTEQZ",
%                                 "JSTRICTEQZ",
%                                 "STTOGLOBALRECORD",
%                                 "STCONSTTOGLOBALRECORD",
%                                 "CREATEREGEXPWITHLITERAL",
%                                 "CLOSEITERATOR"]

% replaced_opcodes = ["JNSTRICTEQ",
%                    "JSTRICTEQ",
%                    "JNE",
%                    "JEQ"]

% skipped_opcodes = additional_deprecated_opcodes + replaced_opcodes

bool HasStringIdOperandDynamic(AbckitIsaApiDynamicOpcode opcode)
{
    switch(opcode) {
        case ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING:
% Panda::instructions.uniq{|i| i.mnemonic}.each do |inst|
%   if !(skipped_opcodes.include? inst.mnemonic.upcase)
%     res = Opcodes.find{|opc| opc.bc_opcode == inst.mnemonic }
%     string_id_operand = inst.operands.find {|op| op.string_id?}
%     if not res.nil? and not string_id_operand.nil?
        case <%= res.abckit_opcode %>:
%     end
%   end
% end
            return true;
        default:
            return false;
    }
}

int GetStringIdOperandIndexDynamic(AbckitIsaApiDynamicOpcode opcode)
{
    switch(opcode) {
        case ABCKIT_ISA_API_DYNAMIC_OPCODE_LOADSTRING:
            return 0;
% Panda::instructions.uniq{|i| i.mnemonic}.each do |inst|
%   res = Opcodes.find{|opc| opc.bc_opcode == inst.mnemonic }
%   string_id_operand = inst.operands.find {|op| op.string_id?}
%   if not res.nil? and not string_id_operand.nil?
        case <%= res.abckit_opcode %>:
            return <%= inst.operands.find_index(string_id_operand) %>;
%   end
% end
        default:
            return -1;
    }
}

bool HasLiteralArrayIdOperandDynamic(AbckitIsaApiDynamicOpcode opcode)
{
    switch(opcode) {
% Panda::instructions.uniq{|i| i.mnemonic}.each do |inst|
%   res = Opcodes.find{|opc| opc.bc_opcode == inst.mnemonic }
%   litarr_id_operand = inst.operands.find {|op| op.literalarray_id?}
%   if not res.nil? and not litarr_id_operand.nil?
    case <%= res.abckit_opcode %>:
%   end
% end
        return true;
    default:
        return false;
    }
}

int GetLiteralArrayIdOperandIndexDynamic(AbckitIsaApiDynamicOpcode opcode)
{
    switch(opcode) {
% Panda::instructions.uniq{|i| i.mnemonic}.each do |inst|
%   res = Opcodes.find{|opc| opc.bc_opcode == inst.mnemonic && !inst.operands.find {|op| op.literalarray_id?}.nil? }
%   litarr_id_operand = inst.operands.find {|op| op.literalarray_id?}
%   if not res.nil? and not litarr_id_operand.nil?
        case <%= res.abckit_opcode %>:
            return <%= inst.operands.find_index(litarr_id_operand) %>;
%   end
% end
        default:
            return -1;
    }
}
